//
// CrackClassLoader
//
// Copyright (C) jextra.net.
//
//  This file is part of the Crack build system.
//
//  The Crack build system is free software; you can redistribute it 
//  and/or modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The Crack build system is distributed in the hope that it will be 
//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the Crack build system; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.crack.core;

import java.io.*;
import java.net.*;

/**
 * <p>
 * This is a dynamic classloader that adds given files to its search path.
 * </p>
 */
public class CrackClassLoader extends URLClassLoader
{
    // ============================================================
    // Constructors
    // ============================================================

    public CrackClassLoader()
        throws MalformedURLException
    {
        // Need to pass in something for superclass.
        super( new URL[]
        {}, CrackClassLoader.class.getClassLoader() );
    }

    public CrackClassLoader( ClassLoader parent )
        throws MalformedURLException
    {
        // Need to pass in something for superclass.
        super( new URL[]
        {}, parent );
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public void addPath( File file )
        throws MalformedURLException
    {
        addURL( file.toURI().toURL() );
    }

    // ----------
    // protected
    // ----------

    /**
     * Override the parent-first behavior established by java.lang.Classloader.
     */
    @Override
    protected Class<?> loadClass( String name, boolean resolve )
        throws ClassNotFoundException
    {
        // First, check if the class has already been loaded
        Class<?> c = findLoadedClass( name );

        // if not loaded, search the local (child) resources
        if ( c == null )
        {
            try
            {
                c = findClass( name );
            }
            catch ( ClassNotFoundException cnfe )
            {
                // ignore
            }
        }

        // if we could not find it, delegate to parent
        // Note that we don't attempt to catch any ClassNotFoundException
        if ( c == null )
        {
            if ( getParent() != null )
            {
                c = getParent().loadClass( name );
            }
            else
            {
                c = getSystemClassLoader().loadClass( name );
            }
        }

        if ( resolve )
        {
            resolveClass( c );
        }

        return c;
    }

    /**
     * Override the parent-first resource loading model established by
     * java.lang.Classloader with child-first behavior.
     */
    @Override
    public URL getResource( String name )
    {
        URL url = findResource( name );

        // if local search failed, delegate to parent
        if ( url == null )
        {
            url = getParent().getResource( name );
        }
        return url;
    }
}
